/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.activity;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.client.ResponseHandler;
import org.niclab.vanbus.R;
import org.niclab.vanbus.application.VanBusApplication;
import org.niclab.vanbus.database.ApplicationDataBase;
import org.niclab.vanbus.gps.MyLocationManager;
import org.niclab.vanbus.model.BusRoute;
import org.niclab.vanbus.model.SkyTrainLine;
import org.niclab.vanbus.model.SkyTrainLine.SkyTrainRoute;
import org.niclab.vanbus.query.BusRouteQuery;
import org.niclab.vanbus.query.command.DismissDialogCmd;
import org.niclab.vanbus.query.command.ShowDialogCmd;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;


public class VanBusAct extends TabActivity{
	
	private static final String LOG_TAG = "VanBusAct";
	private int dialogId=0;
	
	LocationListener coarseLocListener;
	LocationListener fineLocListener;
	
	private SkyTrainTab skytrainTab;
	private FavoritesTab favTab;
	private RemindersTab reminderTab;
	

	@Override
	public void onCreate(Bundle savedInstanceState){
		
		Log.d(LOG_TAG,"creating VanBusAct activity");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
	    
	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec(getString(R.string.bus_route_tab_title)).setIndicator(getString(R.string.bus_route_tab_title))
	                  .setContent(R.id.bus_route_tab);
	    tabHost.addTab(spec);
	    initializeBusRouteTab();
	    Log.d(LOG_TAG,"added Tab busroute");
		
	    
	 	    
	    //add the skytrain tab
	    skytrainTab = new SkyTrainTab(this);
	    skytrainTab.onCreate();
	  
	    Log.d(LOG_TAG,"skytrain tab added");
	    
	    //add the favorites tab
	    favTab = new FavoritesTab(this);
	    favTab.onCreate();
	    
	    Log.d(LOG_TAG,"favorites tab added");
	    
	    //add the reminders tab
	    reminderTab = new RemindersTab(this);
	    reminderTab.onCreate();
	    Log.d(LOG_TAG,"Reminders tab added");
	    
	    //add settings tab
	    intent=new Intent().setClass(this, SettingsActivity.class);
	    spec = tabHost.newTabSpec(getString(R.string.settings_tab_title)).setIndicator(getString(R.string.settings_tab_title)).setContent(intent);
	    tabHost.addTab(spec);
	    //initializeSettingsTab();
	    Log.d(LOG_TAG,"added Settings Tab");
	    
	    //initialize location
	    initializeLocation();
	    
	}

	
	
	
	
	
	private void initializeLocation() {
		
		
		 MyLocationManager locManager = VanBusApplication.getInstance().getMyLocationManager();
		 if(locManager.isGPSEnabled() ){
			
			 //LocationUtils.getIntance(this.getApplicationContext()).getCurrentLocation(true);
			 locManager.startPinDownMyLocation();
		
		 } else{
			 
			 Toast.makeText(this, "Please turn on your GPS satellite. It may be required by some features!", Toast.LENGTH_LONG).show();
		 }
		 
		
	}



	




	private void initializeBusRouteTab() {
		
		Log.d(LOG_TAG,"initializing the Bus Route Tab");
		Log.d(LOG_TAG, "starting to wire up the Search Button");
		
		final EditText routeText = (EditText)findViewById(R.id.busroute_search_box);
		
		Button searchBtn = (Button)findViewById(R.id.busroute_search_btn);
		searchBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String route = routeText.getText().toString();
				Intent intent = new Intent();
				intent.setClass(VanBusAct.this,PickBusRouteExpandableAct.class);
				intent.putExtra(getString(R.string.intent_currently_searched_bus_route_no),route);
				VanBusAct.this.startActivity(intent);
			}
		});
		
	}
	

	public synchronized int registerDialog(){
		
		return dialogId++;
		
	}
	
	@Override
	public void onPrepareDialog(final int id, final Dialog dialog){
		reminderTab.onPrepareDialog(id, dialog);
		
	}
	
	@Override
	public Dialog onCreateDialog(int id){
		Dialog dialog = null;
		dialog = reminderTab.onCreateDialog(id);
		
		return dialog;
	}
	
	@Override
	public void onDestroy(){
		
		//close the database connection
		Log.d(LOG_TAG,"VanBusAct activity: onDestry()" );
		ApplicationDataBase.closeDatabase();
		
		favTab.onDestroy();
		reminderTab.onDestroy();
		super.onDestroy();
		
		
	}

}
